package com.sdy.web;

import com.alibaba.fastjson.JSONObject;
import com.sdy.po.Company;
import com.sdy.service.CompanyService;
import com.sdy.service.OverViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/company")
@SuppressWarnings("all")
public class CompanyController {

    @Autowired
    CompanyService companyService;
    @Autowired
    OverViewService overViewService;

    /**
     * 加载所所有客户公司的信息
     * @param model
     * @return
     */
    @RequestMapping("/choose")
    public String getAllConpany(Model model){
        model.addAttribute("companyList",companyService.getAll());
        return "manager";
    }

    /**
     * 获得公司的识别码，没有的话要set
     * @param companyId
     * @return
     */
    @RequestMapping("/getCode")
    @ResponseBody
    public JSONObject getCompanyCode(HttpSession session){
        Company company = (Company) session.getAttribute("contactCompany");
        JSONObject jsonObject = new JSONObject();
        //如果没有的话要set
        String companyCode = companyService.getConpanyCodeById(Integer.valueOf(company.getId()),true);
        if ( companyCode == null){
            jsonObject.put("result",false);
            jsonObject.put("msg","获取失败");
            return jsonObject;
        }
        jsonObject.put("result",true);
        jsonObject.put("msg","请在30秒内使用！");
        jsonObject.put("data",companyCode);
        return jsonObject;
    }

    /**
     * 获得公司的识别码
     * @param companyId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getCodeOnly")
    public JSONObject getCompanyCodeOnly(Integer companyId){
        JSONObject jsonObject = new JSONObject();
        //没有的话不set
        System.out.println("1111111" + companyId);
        String companyCode = companyService.getConpanyCodeById(Integer.valueOf(companyId),false);
        System.out.println(companyCode);
        if (companyCode == null){
            jsonObject.put("result",false);
            jsonObject.put("msg","不存在");
            return jsonObject;
        }
        jsonObject.put("result",true);
        jsonObject.put("msg","请在30秒内使用！");
        jsonObject.put("data",companyCode);
        return jsonObject;
    }

    /**
     * 加载实时数据页面
     * @param model
     * @return
     */
    @RequestMapping("/realStatement")
    public String realStatement(Model model,HttpSession session){
        model.addAttribute("parmList",overViewService.findAllOverViewName());
        Company company = (Company)session.getAttribute("contactCompany");
        System.out.println(company);
        model.addAttribute("companyCode",companyService.getConpanyCodeById(company.getId(),false));
        return "pages/realStatement";
    }
}
